package com.andy.mongo;

import java.net.UnknownHostException;
import java.util.Arrays;

import org.mongojack.DBCursor;
import org.mongojack.JacksonDBCollection;
import org.mongojack.WriteResult;

import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;

public class MongojackTest {

	
	public static JacksonDBCollection<BlogPost, String> init() {
		JacksonDBCollection<BlogPost, String> coll = null;
		try {
//			MongoClient mongoClient = new MongoClient("192.168.100.49",27017);
			MongoClient mongoClient = new MongoClient(Arrays.asList(new ServerAddress("192.168.100.49", 28010),
                    new ServerAddress("192.168.100.49", 28011),
                    new ServerAddress("192.168.100.49", 28012)));
			DB db = mongoClient.getDB("tempTest");
			db.authenticate("andy", "andy".toCharArray());
			
			coll = JacksonDBCollection.wrap(db.getCollection("users"), BlogPost.class,String.class);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return coll;
		
	}
	
	public static void main(String[] args) {
		JacksonDBCollection<BlogPost, String> coll = init();
		test1(coll);
	}
	
	public static void test1(JacksonDBCollection<BlogPost, String> coll){
		BlogPost blogPost = new BlogPost();
		blogPost.setName("wenming.cui");
		blogPost.setPublished(true);
		blogPost.setTags("java");
		WriteResult<BlogPost, String> result = coll.insert(blogPost);
		String id = result.getSavedId();
		BlogPost foundBlogPost = coll.findOneById(id);
		System.out.println(foundBlogPost);
		DBCursor<BlogPost> cursor = coll.find().is("published", true).in("tags", "mongodb", "java", "jackson");
		while (cursor.hasNext()) {
			BlogPost firstObject = cursor.next();
			System.out.println(firstObject);
		}
		
//		DBCursor<BlogPost> cursor = coll.find(DBQuery.or(
//		        DBQuery.is("author", "jsmith"), 
//		        DBQuery.size("comments", 3)));
	}
	
}
